package com.fzn.admin.controller;

import com.fzn.admin.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


import javax.servlet.http.HttpSession;

/**
 * @program: spring-boot
 * 描述：
 * @author: fzn
 * @create: 2022-03-10 12:58
 **/
@Slf4j
@Controller
public class IndexController {
    /**
     * 去登录页
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model) {
        if (!StringUtils.isEmpty(user.getUserName()) && "123456".equals(user.getPassword())){
            session.setAttribute("loginUser",user);
            // 登录成功 重定向到main页面
            return "redirect:main.html";
        }else {
            // 登录失败
            model.addAttribute("msg","账号密码错误");
            return "login";
        }
    }

    @GetMapping("main.html")
    public String mainPage(HttpSession session, Model model) {
        log.info("当前方法是{}","mainPage");

        // 是否登录
        // Object loginUser = session.getAttribute("loginUser");
        // if (loginUser != null) {
        //     return "main";
        // }else {
        //     model.addAttribute("msg","请重新登录");
        //     return "login";
        // }

        return "main";
    }

}